#include <stdio.h>
#include <stdlib.h>

typedef struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
} TreeNode;

typedef struct Queue {
    TreeNode *data[100];  // 假设树最多100个节点
    int front, rear;
} Queue;

void enqueue(Queue *queue, TreeNode *node) {
    queue->data[queue->rear++] = node;
}

TreeNode* dequeue(Queue *queue) {
    return queue->data[queue->front++];
}

int isEmptyQueue(Queue *queue) {
    return queue->front == queue->rear;
}

void levelOrderTraversal(TreeNode *root) {
    if (!root) return;

    Queue queue;
    queue.front = 0;
    queue.rear = 0;

    enqueue(&queue, root);

    while (!isEmptyQueue(&queue)) {
        TreeNode *node = dequeue(&queue);
        printf("%d ", node->val);

        if (node->left) enqueue(&queue, node->left);
        if (node->right) enqueue(&queue, node->right);
    }
}
